/**
 * @Filename: billboard.cpp
 * @Description:
 * @Author: chengxiaojun
 * @Email: csforgood@gmail.com
 * @Time: 2011/11/11
 */
#include <billboard.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <log.h>
#include <iostream>

using namespace boost::interprocess;
using namespace std;

BillBoard::BillBoard():m_memory_pool(NULL) {
}

BillBoard::~BillBoard() {

}
int BillBoard::create() { 

        shared_memory_object::remove("worktest");
	
	m_memory_pool = new managed_shared_memory(create_only, "worktest", 10240);
	if (NULL == m_memory_pool)
	{
		ERROR_GLOG("create billboard error",1);
		//printf("create billboard error\n");
		return 1;
	}
	m_pdata = m_memory_pool->construct<KV>("KV map")();

}
void BillBoard::set(std::string key, int64_t value){
        (*m_pdata)[key] = value;
}
void BillBoard::add(std::string key, int64_t value){
	std::map<string,int64_t>::iterator ii = m_pdata->find(key);
	if(ii == m_pdata->end()) {
		(*m_pdata)[key] = value;
	} else {
		(*m_pdata)[key] = value + (*ii).second;
	}
}

int64_t BillBoard::get(std::string key) {
	std::map<string,int64_t>::iterator ii = m_pdata->find(key);
	if(ii == m_pdata->end()) {
		return 0;
	} else {
		return (*ii).second;
	}
}

void  BillBoard::show(string key) {
	std::map<string,int64_t>::iterator ii = m_pdata->find(key);
	if(ii == m_pdata->end()) {
		return;
	} else {
		cout << key << ":" <<  (*ii).second<< endl;
	}
}
void  BillBoard::show() {
	for( std::map<string,int64_t>::iterator ii=m_pdata->begin(); ii!=m_pdata->end(); ++ii) {
		string key =  (*ii).first;
		int64_t value = (*ii).second;
		cout << key << ":" << value<< endl;
	}
}
